using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MobTargetingController:MonoBehaviour {

	public bool assumeFromWeapon = true;
	public bool targetFriendly = false;
	public bool targetDamaged = false;
	public bool targetFar = false;

	float GetTargetPriority(Mob self,Mob target) {

		bool _targetFriendly = targetFriendly;
		bool _targetDamaged = targetDamaged;
		bool _targetFar = targetFar;

		if(assumeFromWeapon) {
			var weaponSlot = GetComponentInChildren<WeaponSlot>(false);
			if(weaponSlot) {
				if(weaponSlot.weapon.stat.damage<0) {
					_targetFriendly=true;
					_targetDamaged=true;
				}
			}

		}

		bool isDamager = self.lastDamageMob==target;
		float result = (_targetFriendly ? 10 : -10)*(FactionManager.GetRelation(self.faction,target.faction)-(isDamager ? 2 : 0));
		if(_targetDamaged&&target.HP>=target._maxHP) result-=10000;

		if(result<=0) return result;
		float distance = (self.position-target.position).magnitude;
		if(_targetFar) result+=Mathf.Max(0,distance/20);
		else result+=Mathf.Max(0,1-distance/20);
		return result;
	}

	private void Start() {
		GetComponent<Mob>().getTargetPriority=GetTargetPriority;
	}
}
